package cn.kust.serviceinterestpoint.controller;


import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.serviceinterestpoint.entity.InterestCategory;
import cn.kust.serviceinterestpoint.service.InterestCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author microTourism
 * @since 2022-07-25
 */
@Api(tags = "分类管理模块")
@RestController
@RequestMapping("/serviceinterestpoint/interest-category")
public class InterestCategoryController {

    //把service注入
    @Autowired
    private InterestCategoryService interestCategoryService;

    //1查询分类表所有的数据
    //rest风格
    @ApiOperation(value = "所有分类列表")
    @GetMapping("findAll")
    public ResponseResult<List> findAllCategory() {
        //调用service的方法实现查询所有的操作
        List<InterestCategory> list = interestCategoryService.list(null);
        return ResponseResult.success(list);
    }


    //2根据ID删除分类
    @GetMapping("/delete/{id}")
    @ApiOperation("根据id删除分类")
    @ApiImplicitParam(value = "删除分类的id", name = "id", required = true, example = "1")
    public ResponseResult removeCategoryById(@PathVariable Long id) {
        boolean flag = interestCategoryService.removeById(id);
        if (flag) {
            return ResponseResult.success("分类已删除");
        } else {
            return ResponseResult.fail(404, "删除分类失败");//失效
        }
    }

    //3根据分类Id进行查询
    @ApiOperation("据分类Id进行查询")
    @GetMapping("getCategory/{id}")
    public ResponseResult getCategoryById(@ApiParam(name = "id", value = "分类id", required = true) @PathVariable long id) {
        InterestCategory byId = interestCategoryService.getById(id);
        return ResponseResult.success(byId);
    }

    //4添加分类接口的方法
    //结合前端数据来
    @ApiOperation("添加分类")
    @PostMapping("addCategory")
    public ResponseResult addCategory(@RequestBody InterestCategory interestCategory) {
        boolean save = interestCategoryService.save(interestCategory);
        if (save) {
            return ResponseResult.success();
        } else {
            return ResponseResult.fail(200, "分类已经存在请重新添加");
        }
    }

    //5分类修改功能
    @ApiOperation("分类修改功能")
    @PostMapping("updateCategory")
    public ResponseResult updateCategory(@RequestBody InterestCategory interestCategory) {
        boolean update = interestCategoryService.updateById(interestCategory);
        if (update) {
            return ResponseResult.success();
        } else {
            return ResponseResult.fail(404, "分类的名字重复，修改失败");
        }
    }

    //6分页查询分类的方法
    //current当前页
    //size每页的记录数
    @ApiOperation(value = "分页查询分类")//（从current第1页开始）
    @GetMapping("pageCategory/{current}/{size}")
    public ResponseResult pageList(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                   @ApiParam(name = "size", value = "每页的记录数", required = true) @PathVariable long size) {
        //创建page对象
        Page<InterestCategory> pageCategory = new Page<>(current, size);
        //调用方法
        //自动调用方法把pageTeacher里的所有的数据封装到里面 //从数据库拿到数据
        interestCategoryService.page(pageCategory, null);
        long total = pageCategory.getTotal();//总的页数
        List<InterestCategory> records = pageCategory.getRecords();//得到当前页码的数据list集合

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        return ResponseResult.success(map);
    }
}

